package oj;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LC46 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list=new ArrayList<>();
        LinkedList<Integer> path=new LinkedList<>();
        int n=nums.length;
        boolean[] used=new boolean[n];
        dfs(list,path,nums,n,used);
        return list;

    }
    public void dfs(List<List<Integer>> list,LinkedList<Integer> path,int[] nums,int len,boolean[] used){
        if(path.size()==len){
            list.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<len;i++){
            if(used[i]){
                continue;
            }else{
                used[i]=true;
                path.add(nums[i]);
                dfs(list,path,nums,len,used);
                path.removeLast();
                used[i]=false;
            }
        }
    }
}
